/**
 * 
 */
package de.fsujena.chat_pi.server.userInterface;

import java.text.DateFormat;
import java.util.Date;

import javax.swing.JOptionPane;



import de.fsujena.chat_pi.ressources.message.TextMessage;
import de.fsujena.chat_pi.server.Server;
import de.fsujena.chat_pi.server.userInterface.GUI.MainWindow;

/**
 * @author je65meq
 *
 */
public class GraphicalUI implements UserInterface {
	DateFormat df;
	Server serverObject;
	MainWindow window;
	
	public GraphicalUI(Server server) {
		this.serverObject = server;
		
		df = DateFormat.getTimeInstance( DateFormat.SHORT );
	}

	/* (non-Javadoc)
	 * @see de.fsujena.chat_pi.server.userInterface.UserInterface#postError(java.lang.String)
	 */
	@Override
	public void postError(String str) {
		// TODO Auto-generated method stub
		JOptionPane.showMessageDialog(null, str, "Error", JOptionPane.ERROR_MESSAGE);
	}

	/* (non-Javadoc)
	 * @see de.fsujena.chat_pi.server.userInterface.UserInterface#postInfo(java.lang.String)
	 */
	@Override
	public void postInfo(String str) {
		// TODO Auto-generated method stub
		window.postMessage(df.format(new Date()), "INFO", str);
	}

	/* (non-Javadoc)
	 * @see de.fsujena.chat_pi.server.userInterface.UserInterface#postTextMessage(de.fsujena.chat_pi.ressources.message.TextMessage)
	 */
	@Override
	public void postTextMessage(TextMessage msg) {
		// TODO Auto-generated method stub
		//System.out.println("["+msg.getSenderUsername().toUpperCase()+" ("+df.format(msg.getReceived())+")]" + ">> " + msg.getMessageText());
		window.postMessage(df.format(msg.getReceived()), msg.getSenderUsername(), msg.getMessageText());
	}

	@Override
	public void postOwnTextMessage(String str) {
		// TODO Auto-generated method stub
		window.postOwnMessage(df.format(new Date()), "Server", str);
	}

	/* (non-Javadoc)
	 * @see de.fsujena.chat_pi.server.userInterface.UserInterface#postWarning(java.lang.String)
	 */
	@Override
	public void postWarning(String str) {
		// TODO Auto-generated method stub
		window.postMessage(df.format(new Date()), "WARNING", str);
	}
	
	public void postText(String str){
		window.postText(str);
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		try {
			window = new MainWindow(serverObject);
			//window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateRoomTree(){
		this.window.userRoomTree.updateUI();
		this.window.roomTree.updateUI();
		for(int i=0;i<this.window.roomTree.getRowCount();i++){
			this.window.roomTree.expandRow(i);
		}
		for(int i=0;i<this.window.userRoomTree.getRowCount();i++){
			this.window.userRoomTree.expandRow(i);
		}
	}
}
